package grafica;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;


public class VentanaLogin extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel usuarioLabel = null;
	private JLabel passwordLabel = null;
	private JTextField usuario = null;
	private JPasswordField password = null;
	private JButton login = null;
	private ControladorLogin controlador = null;

	/**
	 * This is the default constructor
	 */
	public VentanaLogin() {
		super();
		controlador = new ControladorLogin();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLocation(250, 150);
		this.setSize(300, 154);
		this.setContentPane(getJContentPane());
		this.addWindowListener(new java.awt.event.WindowAdapter() 
		{
			public void windowClosing(java.awt.event.WindowEvent e) 
			{
				VentanaLogin.this.dispose();
				System.exit(0);
			}
		});
		this.setTitle("Login");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			passwordLabel = new JLabel();
			passwordLabel.setBounds(new java.awt.Rectangle(145,15,120,23));
			passwordLabel.setText("Password:");
			usuarioLabel = new JLabel();
			usuarioLabel.setBounds(new java.awt.Rectangle(13,15,120,23));
			usuarioLabel.setText("Usuario:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(usuarioLabel, null);
			jContentPane.add(passwordLabel, null);
			jContentPane.add(getUsuario(), null);
			jContentPane.add(getPassword(), null);
			jContentPane.add(getLogin(), null);
		}
		return jContentPane;
	}
	/**
	 * This method initializes usuario	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUsuario() {
		if (usuario == null) {
			usuario = new JTextField();
			usuario.setBounds(new java.awt.Rectangle(13,42,120,23));
		}
		return usuario;
	}

	/**
	 * This method initializes password	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getPassword() {
		if (password == null) {
			password = new JPasswordField();
			password.setBounds(new java.awt.Rectangle(145,42,120,23));
		}
		return password;
	}

	/**
	 * This method initializes login	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getLogin() {
		if (login == null) {
			login = new JButton();
			login.setBounds(new java.awt.Rectangle(87,75,102,28));
			login.setText("Ingresar");
			login.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (usuario.getText().isEmpty()){
						JOptionPane.showMessageDialog(jContentPane, "El usuario es requerido", "Aviso", JOptionPane.WARNING_MESSAGE);
					} else {
						if (password.getPassword().length == 0) {
							JOptionPane.showMessageDialog(jContentPane, "El password es requerido", "Aviso", JOptionPane.WARNING_MESSAGE);
						} else {
							try {
								if (controlador.validarUsuario(usuario.getText().trim(), new String(password.getPassword()))) {
									controlador.abrirVentanaPrincipal();
									VentanaLogin.this.dispose();
								} else {
									JOptionPane.showMessageDialog(jContentPane, "Usuario o password incorrectos", "Aviso", JOptionPane.WARNING_MESSAGE);
								}
							} catch (Exception e1) {
								JOptionPane.showMessageDialog(jContentPane, e1.getMessage(), "Error del servidor", JOptionPane.WARNING_MESSAGE);
							}
						}
					}
				}
			});
		}
		return login;
	}

}  //  @jve:decl-index=0:visual-constraint="160,11"
